<?php

/**
 *  合同mo'ban 
 * Created by PhpStorm.
 * User: Apache
 * Date: 2019/10/27
 * Time: 10:02
 */
namespace app\api\controller\basemanage;

use app\api\controller\common\ApiBase;
use PhpOffice\PhpWord\TemplateProcessor;
use app\api\controller\common\CommonPlus;

class Discount extends ApiBase
{

    protected $admin;
    protected $model = null;
    public function _initialize()
    {
        $this->admin = session('admin');
        $this->model = model('Discount');
    }

    /**
     * 折扣列表页
     */
    public function getList()
    {
        if (request()->isPost()) {
            $keyword = $this->request->param('keyword');
            $discount_type = $this->request->param('discount_type');
            $project = $this->request->param('project');
            $page = $this->request->param('page');
            $where = array();
            $page = max(1, intval($page));
            $pagesize = 10;
            $where['a.is_delete'] = 0;

            if (!empty($keyword)) {
                $where['a.title'] = array('like', '%' . $keyword . '%');
            }
            if (!empty($discount_type)) {
                $where['a.discount_type'] = $discount_type;
            }
            if (!empty($project)) {
                $where['a.project'] = $project;
            }

            $list =  $this->model
                    ->alias('a')
                    ->field('a.id,a.title,a.discount_type,a.project,a.create_time,a.is_show,a.discount_num,e.title as e_title,t.title as z_title')
                    ->join('exhibition e','a.project=e.id','left')
                    ->join('discount_type t','a.discount_type=t.id','left')
                    ->where($where)
                    ->limit(($page-1)*$pagesize.','.$pagesize)
                    ->order('a.id desc')
                    ->select();
            $total = $this->model
                    ->alias('a')
                    ->field('a.id,a.title,a.discount_num,a.discount_type,a.project,a.is_show,e.title as e_title,t.title as z_title')
                    ->join('exhibition e','a.project=e.id','left')
                    ->join('discount_type t','a.discount_type=t.id','left')
                    ->where($where)
                    ->count();
            return json(['code' => 1, 'msg' => 'success', 'data' => ['list' => $list?$list:array(), 'total' => $total]]);
        }else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    /**
     * 设置新增
     * @return \think\response\Json|void
     */
    public function add()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $title = isset($post['title']) && $post['title'] != '' ? $post['title'] : null;
            $discount_type = isset($post['discount_type']) && $post['discount_type'] != '' ? $post['discount_type'] : null;
            $project = isset($post['project']) && $post['project'] != '' ? $post['project'] : null;
            $discount_num = isset($post['discount_num']) && $post['discount_num'] != '' ? $post['discount_num'] : null;
            $is_show = isset($post['is_show']) && $post['is_show'] != '' ? $post['is_show'] : 1;
            if(!$title){
                return json(array('code' => '0', 'msg' => '请输入折扣名称'));
            }
            if(!$discount_type){
                return json(array('code' => '0', 'msg' => '请选择折扣类型'));
            }
            if(!$project){
                return json(array('code' => '0', 'msg' => '请选择所属展会'));
            }
            if(!$discount_num){
                return json(array('code' => '0', 'msg' => '请输入折扣'));
            }
            $data['title']=$title;
            $data['discount_type']=$discount_type;
            $data['project']=$project;
            $data['discount_num']=$discount_num;
            $data['is_show']=$is_show;
            $data['create_time']=time();
            $result=$this->model->insertData($data);
            //密码
            
            if($result){
                return json(array('code' => '1', 'msg' => '新增成功'));
            }else{
                return json(array('code' => '0', 'msg' => '获取失败'));
            }
        }else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


    /**
     * 设置编辑
     * 
     * @return \think\response\Json|void
     */
    public function edit()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $id = isset($post['id']) && $post['id'] != '' ? $post['id'] : null;
            $title = isset($post['title']) && $post['title'] != '' ? $post['title'] : null;
            $discount_type = isset($post['discount_type']) && $post['discount_type'] != '' ? $post['discount_type'] : null;
            $project = isset($post['project']) && $post['project'] != '' ? $post['project'] : null;
            $discount_num = isset($post['discount_num']) && $post['discount_num'] != '' ? $post['discount_num'] : null;
            $is_show = isset($post['is_show']) && $post['is_show'] != '' ? $post['is_show'] : 1;
            if(!$id){
                return json(array('code' => '0', 'msg' => '缺少关键主键'));
            }
            if(!$title){
                return json(array('code' => '0', 'msg' => '请输入折扣名称'));
            }
            if(!$discount_type){
                return json(array('code' => '0', 'msg' => '请选择折扣类型'));
            }
            if(!$project){
                return json(array('code' => '0', 'msg' => '请选择所属展会'));
            }
            if(!$discount_num){
                return json(array('code' => '0', 'msg' => '请输入折扣'));
            }
            $data['title']=$title;
            $data['discount_type']=$discount_type;
            $data['project']=$project;
            $data['discount_num']=$discount_num;
            $data['is_show']=$is_show;
            $where['id']=$id;
            $result=$this->model->updateBywhere($data,$where);
            if($result){
                return json(array('code' => '1', 'msg' => '新增成功'));
            }else{
                return json(array('code' => '0', 'msg' => '新增失败'));
            }
        }else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    /**
     * 设置删除
     * 
     */
    public function del()
    {
        if (request()->isPost()) {
            $post = input('post.');
            $id = isset($post['id']) && $post['id'] != '' ? $post['id'] : null;
            if(!$id){
                return json(array('code' => '0', 'msg' => '缺少关键主键'));
            }
            $where['id']=$id;
            $r=$this->model->where($where)->find();
            if(!$r){
                return json(array('code' => '0', 'msg' => '编辑记录不存在'));
            }
            $result=$this->model->deleteWhere($where);
            if($result){
                return json(array('code' => '1', 'msg' => '删除成功'));
            }else{
                return json(array('code' => '0', 'msg' => '删除失败'));
            }
        }else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    //合同模板复用展会列表
    public function projectList(){
        if (request()->isPost()) {
            $where['is_delete'] = 0;
            $list = db('exhibition')
            ->where($where)
            ->order('id desc')
            ->select();
            return json(['code'=>1,'msg'=>'success','data'=>$list?$list:array()]);
        }else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    //合同模板复用项目类型列表
    public function discountList()
    {
        if (request()->isPost()) {
            $where['is_delete']=0;
            $result = db('discount_type')->where($where)->order('id desc')->select();
            if($result){
                return json(array('code' => '1', 'msg' => '获取成功','data'=>$result?$result:array()));
            }else{
                return json(array('code' => '0', 'msg' => '获取失败'));
            }
        }else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


}
